package com.woniu.config;

import com.woniu.common.constant.UserConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨光 微信号:tolryg
 * @time 16:13
 */
@Configuration
public class RabbitConfig {

    //创建Direct类型Exchanger
    @Bean("delayExchange")
    public DirectExchange delayExchange(){
        return new DirectExchange(UserConstants.DELAY_ORDER_EXCHANGE);
    }
    //创建Direct类型Exchanger
    @Bean("dlxExchange")
    public DirectExchange dlxExchange(){
        return new DirectExchange(UserConstants.DLX_ORDER_EXCHANGE);
    }

    @Bean("dlxQueue")
    public Queue dlxQueue(){
        return new Queue(UserConstants.DLX_ORDER_QUEUE,true);
    }

    @Bean("delayQueue")
    public Queue delayQueue(){
        Map<String, Object> arguments = new HashMap();
        //设置死信交换机名字
        arguments.put("x-dead-letter-exchange", UserConstants.DLX_ORDER_EXCHANGE);
        //设置死信交换机的routing key
        arguments.put("x-dead-letter-routing-key", UserConstants.DLX_ORDER_ROUTING_KEY);
        //设置消息在延迟队列中的保存时间
        arguments.put("x-message-ttl",60000);
        return new Queue(UserConstants.DELAY_ORDER_QUEUE,true,false,false,arguments);
    }

    @Bean
    public Binding dlxBinding(){
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with(UserConstants.DLX_ORDER_ROUTING_KEY);
    }
    @Bean
    public Binding delayBinding(){
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(UserConstants.DELAY_ORDER_ROUTING_KEY);
    }

}
